package org.csu.mypetstore.service.impl;


import org.csu.mypetstore.domain.*;
import org.csu.mypetstore.persistence.*;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.OrderService;
import org.csu.mypetstore.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service("OrderService")
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private SequenceMapper sequenceMapper;

    @Autowired
    private LineItemMapper lineItemMapper;

    @Override
    public Order initializeOrder(Account account, Cart cart) {
        Order order = new Order();
        order.initOrder(account, cart);
        return order;
    }

    private int getNextOrderId() {
        final String sequenceName = "ordernum";
        Sequence sequence = sequenceMapper.getSequence(new Sequence(sequenceName, -1));

        // 如果序列不存在，初始化并插入
        if (sequence == null) {
            sequenceMapper.insertSequence(new Sequence(sequenceName, 1000)); // 初始值设为1000
            sequence = new Sequence(sequenceName, 1000);
        }

        int nextId = sequence.getNextId();
        sequenceMapper.updateSequence(new Sequence(sequenceName, nextId + 1)); // 更新为 1001
        return nextId; // 返回当前值 1000
    }

    @Override
    @Transactional
    public void submitOrder(Order order) {

        // 生成订单ID
        int orderId = getNextOrderId();
        order.setOrderId(orderId);

        // 插入主订单
        orderMapper.insertOrder(order);

        // 插入订单明细前检查 lineItems 数量
        System.out.println("LineItems count: " + order.getLineItems().size()); // 应等于购物车商品数

        // 插入订单明细
        int lineNumber = 1;
        for (LineItem item : order.getLineItems()) {
            item.setOrderId(order.getOrderId());
            item.setLineNumber(lineNumber++);

            // 插入明细
            lineItemMapper.insertLineItem(item);
        }

    }

//    private AccountOrder convertToAccountOrder(LineItem item) {
//        AccountOrder accountOrder = new AccountOrder();
//        //accountOrder.setOrderId(item.getOrderId());
//        //accountOrder.setLineNumber(item.getLineNumber());
//        accountOrder.setProductid(item.getItem().getProductId());
//        accountOrder.setItemid(item.getItemId());
//        accountOrder.setQuantity(item.getQuantity());
//        accountOrder.setTotalcost(item.getTotal().toString());
//        return accountOrder;
//    }

    @Override
    public List<Order> getOrderHistory(String username) {
        return orderMapper.getOrdersByUsername(username);
    }

    @Override
    public void updateShippingAddress(Order original, Order newShipping) {
        original.setShipAddress1(newShipping.getShipAddress1());
        original.setShipAddress2(newShipping.getShipAddress2());
        original.setShipCity(newShipping.getShipCity());
        original.setShipState(newShipping.getShipState());
        original.setShipZip(newShipping.getShipZip());
        original.setShipCountry(newShipping.getShipCountry());
        orderMapper.updateShippingAddress(original);
    }

    @Override
    public Order getOrderDetails(int orderId) {
        return orderMapper.getOrderWithItems(orderId);
    }
}
